package poi;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Test {

    public static void main(String[] args) {
        try {
            // 读取Excel文件
            FileInputStream file = new FileInputStream("C:\\Users\\李博飞\\Desktop\\test.xlsx");
            Workbook workbook = new XSSFWorkbook(file);
            Sheet sheet = workbook.getSheetAt(0);

            // 循环遍历所有行，修改第二列的单元格内容
            for (Row row : sheet) {
                Cell cell = row.getCell(1); // 第二列单元格，索引从0开始
                if(null == cell){
                    continue;
                }
                System.out.println(cell.getStringCellValue());
                System.out.println("-----------------------------------------------");
                String modifiedText = cell.getStringCellValue().replaceAll("地址：.*?(\\d+[号楼\\)]).*?\n", "");

                cell.setCellValue(modifiedText); // 设置新的单元格内容
            }

            // 保存修改后的Excel文件
            FileOutputStream outputFile = new FileOutputStream("C:\\Users\\李博飞\\Desktop\\test1.xlsx");
            workbook.write(outputFile);
            outputFile.close();

            System.out.println("Excel文件操作完成。");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
